package cn.edu.cug.cs.gtl.apis.ui.imageuis;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.UIApp;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageUIApp extends UIApp {

    private final JLabel imageLabel = new JLabel();

    public ImageUIApp(AbstractApplication theApp) {
        super(theApp);
    }

    @Override
    public JComponent initializeUI() {
        return imageLabel;
    }

    public void redRawJpgUI(FSDataInputStream fis, String fileType) throws IOException {
        // 将FSDataInputStream写入临时文件
        File tempFile = null;
        // 去掉文件类型前面的点，以确保文件类型字符串正确
        if (fileType.startsWith(".")) {
            fileType = fileType.substring(1);
        }

        //根据文件类型选择适当的File实现
        if ("jpg".equalsIgnoreCase(fileType)) {
            tempFile = File.createTempFile("imageTemp", "jpg");
        } else if ("png".equalsIgnoreCase(fileType)) {
            tempFile = File.createTempFile("imageTemp", "png");
        } else {
            throw new IllegalArgumentException("Unsupported file type: " + fileType);
        }

        tempFile.deleteOnExit();

        try (FileOutputStream fos = new FileOutputStream(tempFile)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, length);
            }
        }

        // 从临时文件读取图像
        BufferedImage image = ImageIO.read(tempFile);
        if (image != null) {
            ImageIcon imageIcon = new ImageIcon(image);
            imageLabel.setIcon(imageIcon);

            //设置图片居中显示
            imageLabel.setHorizontalAlignment(SwingConstants.CENTER); // 水平居中
            imageLabel.setVerticalAlignment(SwingConstants.CENTER); // 垂直居中

            // 添加鼠标滚轮监听器以实现缩放功能
            imageLabel.addMouseWheelListener(new MouseWheelListener() {
                @Override
                public void mouseWheelMoved(MouseWheelEvent e) {
                    // 获取滚轮的旋转量
                    int notches = e.getWheelRotation();
                    // 获取当前图片
                    Image image = imageIcon.getImage();

//                    // 计算新的宽度和高度
//                    double scale = 1.0 - (notches * 0.1); // 每个刻度改变10%的大小
//                    int newWidth = (int) (image.getWidth(null) * scale);
//                    int newHeight = (int) (image.getHeight(null) * scale);

                    // 计算新的宽度和高度(2.0为最大放大比例，0.5为最小缩小比例，可以自行修改设置)
                    int newWidth = (int) (image.getWidth(null) * (notches < 0 ? 2.0 : 0.5));
                    int newHeight = (int) (image.getHeight(null) * (notches < 0 ? 2.0 : 0.5));

                    // 重新缩放图片
                    Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
                    // 更新 JLabel 的 ImageIcon
                    imageLabel.setIcon(new ImageIcon(scaledImage));
                }
            });

            imageLabel.removeAll();
            imageLabel.revalidate();
            imageLabel.repaint();
        } else {
            throw new IOException("Could not read the image");
        }
    }

    @Override
    public boolean isValid() {
        return false;
    }

    @Override
    public Type getType() {
        return Type.IMAGE_UI_APP;
    }
}
